package com.taptech.doufu.ui.view.search;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.services.DiaobaoSearchService;
import com.taptech.doufu.ui.view.HomeSearchContentListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnLoadAndRefreshListener;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnScrollToBottomListener;
import com.taptech.doufu.ui.viewholder.personalcenter.MessageViewHolderFactory;
import com.taptech.doufu.ui.viewholder.personalcenter.SearchListViewAdapter;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 小故事
 */
public class SearchStoryView implements HttpResponseListener ,SearchBaseView{
    private Activity activity;
    private View rootView ,loadView,nullContentLayout;
    private HomeSearchContentListView contents;// 搜索后的内容list
    private SearchListViewAdapter adapter;
    private String currentKey = "";
    private int pageIndex;
    private int oldPage;
    List<MineAbstractBean> mDataList;
    private MessageViewHolderFactory viewHolderFactory;

    public SearchStoryView(Activity activity, View rootView) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.rootView = rootView;
        initView();
    }

    private void initView() {
        loadView = rootView.findViewById(R.id.empty_load_root_layout);
        nullContentLayout = rootView.findViewById(R.id.empty_load_layout_null_content);
        mDataList = new ArrayList<MineAbstractBean>();
        viewHolderFactory = new MessageViewHolderFactory();
        adapter = new SearchListViewAdapter(this.activity, mDataList, viewHolderFactory);
        adapter.setSinglePage(1);
        contents = (HomeSearchContentListView) rootView.findViewById(R.id.search_activity_article_list);
        contents.setAdapter(adapter);
        contents.setRefreshable(false);
        contents.setLoadmoreable(true);
        contents.getFootView().setVisibility(View.VISIBLE);
        contents.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                try {
                    ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(activity.getCurrentFocus()
                            .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
        contents.setOnLoadAndRefreshListener(new OnLoadAndRefreshListener() {

            @Override
            public void onRefresh() {}

            @Override
            public void loadMore() {}
        });
        contents.setOnScrollToBottomListener(new OnScrollToBottomListener() {

            @Override
            public void OnScrollToBottom() {
                contents.setEnabled(false);
                if (oldPage != pageIndex) {
                    oldPage = pageIndex;
                    DiaobaoSearchService.getInstance().searchArticle(currentKey, pageIndex, SearchStoryView.this, "18","","","",1);
                }
            }
        });

    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {

        try {
            switch (handleType) {
                case DiaobaoSearchService.HANDLE_TYPE_SEARCH_ARTICLE:
                    if(loadView.getVisibility() == View.VISIBLE){
                        loadView.setVisibility(View.GONE);
                    }
                    if (response.getStatus() == 0) {
                        if (response.getRequestData() == null || !currentKey.equals(response.getRequestData()))
                            return;
                        pageIndex = response.getPageIndex();
                        JSONArray jsonArray = (JSONArray) response.getData();
                        List<? extends MineAbstractBean> dataList = viewHolderFactory.getDataBeanFromJSONArray(jsonArray);
                        mDataList.addAll(dataList);
                        if (pageIndex == 0) {
                            contents.setFull(false);
                        }
                        adapter.setSearchKeyWord(currentKey);
                        adapter.notifyDataSetChanged();
                        oldPage = pageIndex;
                        pageIndex = pageIndex + 1;
                        contents.loadMoreData();
                        if (dataList != null && dataList.size() == 0) {
                            contents.getFootView().setVisibility(View.VISIBLE);
                            contents.setFull(true);
                            contents.loadMoreComplete();
                        }
                        if(mDataList.size() == 0){
                            loadView.setVisibility(View.VISIBLE);
                            nullContentLayout.setVisibility(View.VISIBLE);
                        }
                    } else {
                        contents.setEnabled(true);
                        oldPage = -1;
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void searchContent(String searchContent) {
        pageIndex = 0;
        this.currentKey = searchContent;
        contents.getFootView().setVisibility(View.GONE);
        if (mDataList != null && mDataList.size() > 0){
            mDataList.clear();
        }
        cleanData();
        loadView.setVisibility(View.VISIBLE);
        nullContentLayout.setVisibility(View.GONE);
        DiaobaoSearchService.getInstance().searchArticle(searchContent, pageIndex, this, "18","","","",1);
    }



    @Override
    public void hideFilter(boolean isReset) {
    }

    @Override
    public void cleanData(){
        if (adapter != null) {
            adapter.clearDateSource();
        }
    }
}
